<?php
abstract class AbsCatModel extends AbsCatBase {
	private $modelName;
	protected $driver; //db驱动器对象
	
	public function __construct() {
		$this->driver = Db::getInstance();
	}
	
	/**
	 * 获取数据库驱动对象，较数据库操作对象高级
	 */
	public function getDriver() {
		return $this->driver;
	}
	
	/**
	 * 获取数据库操作对象，用于实现最底层的操作
	 */
	public function getDbHandle($read = true) {
		return $this->driver->getDbHandle($read);
	}
	
	/**
	 * get model name
	 * use this function, you can get model class name
	 */
	public function getName() {
		if(null === $this->modelName) $this->modelName = get_class($this);
		return $this->modelName;
	}
	
	/**
	 * 获取数据
	 */
	public function select($field = '*', $where = '', $order = '', $limit = '', $distinct = false, $lock = false) {
		$options = array(
			'table' => $this->getTableName(),
			'field' => $field,
			'where' => $where,
			'order' => $order,
			'limit' => $limit,
			'distinct' => $distinct,
			'lock' => $lock,
		);
		
		return $this->driver->select($options);
	}
	
	/**
	 * 使用底层数据库驱动器对象获取数据
	 */
	public function selectBySql($sql) {
		return $this->driver->query($sql);
	}
	
	/**
	 * 使用底层数据库驱动器对象插入/更新数据
	 */
	public function executeBySql($sql) {
		return $this->driver->execute($sql);
	}
	
	/**
	 * 插入数据
	 */
	public function insert($data, $lock = false) {
		$options = array(
			'table' => $this->getTableName(),
			'lock' => $lock,
		);
		
		return $this->driver->insert($data, $options);
	}
	
	/**
	 * 更新数据
	 */
	public function update($data, $where = '', $lock = false) {
		$options = array(
			'table' => $this->getTableName(),
			'lock' => $lock,
			'where' => $where,
		);
		
		return $this->driver->update($data, $options);
	}
	
	/**
	 * 删除数据
	 */
	public function delete($where) {
		$options = array(
			'table' => $this->getTableName(),
			'where' => $where,
		);
		
		return $this->driver->delete($options);
	}
	
	/**
	 * 获取查询次数
	 */
	public function getQueryTimes() {
		return $this->driver->Q();
	}
	
	/**
	 * 获取写入次数
	 */
	public function getWriteTimes() {
		return $this->driver->W();
	}
	
	/**
	 * 获取最后执行的sql语句
	 */
	public function getLastSql() {
		return $this->driver->getLastSql();
	}
	
	/**
	 * 获取最近的错误信息
	 */
	public function getError() {
		return $this->driver->getError();
	}
	
	/**
	 * 转义sql字符串
	 */
	public function escape($str) {
		return $this->driver->escape_string($str);
	}
	
	/**
	 * 开始事务
	 */
	public function startTrans() {
		return $this->driver->startTrans();
	}
	
	/**
	 * 提交事务
	 */
	public function commit() {
		return $this->driver->commit();
	}
	
	/**
	 * 回退事务
	 */
	public function rollback() {
		return $this->driver->rollback();
	}
	
	/**
	 * 获取表中的字段列表
	 */
	public function getFields() {
		return $this->driver->getFields($this->getTableName());
	}
	
	/**
	 * 获取数据库中的表列表
	 */
	public function getTables($dbName='') {
		return $this->driver->getTables($dbName);
	}
	
	abstract public function getTableName();
}
?>